#ifdef SETTINGS_USE_DBC
#include "tsettings.h"
#include "dbtabsettings.h"
#include <QDebug>
#include <QFileInfo>

TSettings::TSettings(const QString &fileName, TSettings::Format format, QObject *parent):QObject(parent)
{
    Q_UNUSED(format);
    QFileInfo info(fileName);
    QString str = info.fileName().replace('.','_');
    mDBTabSettings = DBTabSettings::getObj(str);
}

TSettings::~TSettings()
{

}

void TSettings::clear()
{
    mKeyQuenu.clear();
}

void TSettings::beginGroup(const QString &prefix)
{
    KeyInfo info;
    info.key = prefix;
    info.key.replace("//","/");
    while(info.key.endsWith('/'))
    {
        info.key.chop(1);
    }
    info.ktype = KT_GROUP;

    mKeyQuenu.push(info);
}

void TSettings::endGroup()
{
    if(mKeyQuenu.last().ktype != KT_GROUP)
    {
        qWarning("not expect endGroup");
    }
    mKeyQuenu.pop();


}



QString TSettings::group() const
{
    QString str;
    foreach(const KeyInfo &info,mKeyQuenu){
        str.append(info.key);
        str.append('/');
    }
    str.chop(1);
    return str;
}

int TSettings::beginReadArray(const QString &prefix)
{
    KeyInfo info;
    info.key = prefix;
    info.key.replace("//","/");
    while(info.key.endsWith('/'))
    {
        info.key.chop(1);
    }
    info.ktype = KT_ARRAY;

    mKeyQuenu.push(info);
    return mDBTabSettings->getMaxIndex(group());
}

void TSettings::beginWriteArray(const QString &prefix, int size)
{
    Q_UNUSED(size);
    KeyInfo info;
    info.key = prefix;
    info.key.replace("//","/");
    while(info.key.endsWith('/'))
    {
        info.key.chop(1);
    }
    info.ktype = KT_ARRAY;
    mKeyQuenu.push(info);

}

void TSettings::endArray()
{
    if(mKeyQuenu.last().ktype == KT_GROUP)
    {
        qWarning("not expect endArray");
    }
    if(mKeyQuenu.last().ktype == KT_INDEX)
    {
        mKeyQuenu.pop();
    }
    mKeyQuenu.pop();
}

void TSettings::setArrayIndex(int i)
{
    KeyType type = mKeyQuenu.last().ktype;
    KeyInfo info;
    info.key = QString("%1").arg(i,4,10,QLatin1Char('0'));
    info.ktype = KT_INDEX;
    if(type == KT_ARRAY){
        mKeyQuenu.push(info);
    }else if(type==KT_INDEX){
        mKeyQuenu.pop();
        mKeyQuenu.push(info);
    }else{
        qWarning("not begin a array");
    }
}

//QStringList TSettings::allKeys() const //not implement
//{
//    QStringList sl;
//    return sl;
//}

void TSettings::setValue(const QString &key, const QVariant &value)
{
    QString tkey = key;
    tkey.replace("//","/");
    while(tkey.endsWith('/'))
    {
        tkey.chop(1);
    }
    tkey = group() + '/'+key;
    mDBTabSettings->setValue(tkey,value);

}

QVariant TSettings::value(const QString &key, const QVariant &defaultValue) const
{
    QString tkey = key;
    tkey.replace("//","/");
    while(tkey.endsWith('/'))
    {
        tkey.chop(1);
    }
    tkey =  group() + '/'+key;
    return mDBTabSettings->getValue(tkey,defaultValue);
}



//void TSettings::sync()
//{

//}

//TSettings::Status TSettings::status() const
//{
//    return TSettings::NoError;
//}
#endif
